/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package phd.changeidentifiers.testing.display;

import java.io.File;
import java.sql.SQLException;
import phd.changeidentifiers.testing.display.database.DataSourceNotInitialisedException;
import phd.changeidentifiers.testing.display.database.DatabaseConnection;
import phd.changeidentifiers.testing.display.frames.GraphFrame;
import phd.changeidentifiers.testing.display.frames.MainFrame;
import phd.changeidentifiers.testing.display.graphs.GraphTestAreaDifference;
import phd.changeidentifiers.testing.display.graphs.GraphTimeStepTimeDifferences;
import phd.changeidentifiers.testing.display.objects.ChangeIdentifierSetDBObject;
import phd.changeidentifiers.testing.display.objects.TestDBObject;
import phd.changeidentifiers.testing.display.objects.TestRetrievalEngine;

/**
 *
 * @author Max
 */
public class TestDisplayApplication {

    public final static boolean GRAYSCALE = true;
    public final static boolean LARGE = true;

    private MainFrame frame;
    private TestDBObject currentTest;
    public TestDisplayApplication(){
        frame = new MainFrame(this);
        try {
            boolean onSecam = false;
            if(new File("/olib1/ecs_ug/cspg/mpd209/").isDirectory()){
                onSecam = true;
            }
            if(onSecam){
                DatabaseConnection.initialise("secamsql.ex.ac.uk", "pccat", "pccat", "pccat");
            }else{
                DatabaseConnection.initialise("localhost", "change-identifier", "ci-user", "changes");
            }
            TestDBObject[] tests = TestRetrievalEngine.getTests();
            frame.setTests(tests);
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.exit(1);
        }catch (DataSourceNotInitialisedException ex) {
            ex.printStackTrace();
            System.exit(1);
        }

        /*Get Tests*/
        frame.setVisible(true);
    }
    public void testSelected(TestDBObject test){
        try {
            if(!test.isFilled()) TestRetrievalEngine.fillTest(test);

            frame.setTestTitle(test.getTitle());
            frame.setTestDescription(test.getDescription());
            frame.setTestTimeTaken(test.getTimeTaken());
            frame.setTestTimeTakenWithoutChangeIdentiers(test.getTimeTakenWithoutChangeIdentifiers());
            frame.setFootprintAlgorithm(test.getFootprintAlgorithmClass());
            frame.setTimeSteps(test.getTimeSteps());
            frame.setTimeStepsWithoutChangeIdentifiers(test.getTimeStepsWithoutChangeIdentifiers());
            frame.resetTimeStepCounter();
            ChangeIdentifierSetDBObject ciSet = test.getChangeIdentifierSet();
            frame.setChangeIdentifierSetName(ciSet.getName());
            frame.setChangeIdentifierSetVersion(ciSet.getVersion());
            currentTest = test;
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (DataSourceNotInitialisedException ex) {
            ex.printStackTrace();
        }
    }
    public void generateGraphs(){
        GraphFrame areaDiff = new GraphFrame(new GraphTestAreaDifference(
                currentTest.getTimeSteps(),
                currentTest.getRedrawCount()));
        areaDiff.setTitle("Area Difference");
        areaDiff.setSize(600, 400);
        areaDiff.setVisible(true);
        GraphFrame timeDiff = new GraphFrame(new GraphTimeStepTimeDifferences(
                currentTest.getTimeSteps(),
                currentTest.getTimeStepsWithoutChangeIdentifiers(),
                currentTest.getRedrawCount()));
        timeDiff.setTitle("Time Differences");
        timeDiff.setSize(600, 400);
        timeDiff.setVisible(true);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestDisplayApplication();
            }
        });

    }

}
